import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( '../views/Home.vue'),
    children: [
      {
        path: '/customer/create',
        name: 'create',
        component: () => import( '../views/customer/Create.vue'),
      },
      {
        path: '/customer/list',
        name: 'list',
        component: () => import( '../views/customer/List.vue'),
      },  
      {
        path: '/customer/dashboard',
        name: 'Dashboard',
        component: () => import( '../views/customer/Dashboard.vue'),
      },
      {
        path: '/user/manage',
        name:'manage',
        component: () => import('../views/UserManage.vue'),
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(to.name != 'Login' && !this.$store.state.currentUser.username){
    next({name:"Login"});
  }
})

export default router
